﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLThuChi.Models;

namespace QLThuChi.Reports
{
    public partial class FrmPrintDoanhThu : Form
    {
        private List<DoanhThu> _lstDoanhThu;
        private string _congtrinh;

        public FrmPrintDoanhThu(List<DoanhThu> lstItem, string congtrinh)
        {
            InitializeComponent();
            _lstDoanhThu = lstItem;
            _congtrinh = congtrinh;
        }

        private void FrmPrintDoanhThu_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        public void LoadData()
        {
            long tongthu = 0;
            long tongchi = 0;
            long doanhthu = 0;

            var dt = new DataTable();
            dt.Columns.Add("STT");
            dt.Columns.Add("GiaiDoan");
            dt.Columns.Add("TongThu");
            dt.Columns.Add("TongChiLuong");
            dt.Columns.Add("TongChiVLXD");
            dt.Columns.Add("Loi");
            dt.Columns.Add("Lo");

            for (int i = 0; i < _lstDoanhThu.Count; i++)
            {
                dt.Rows.Add((i + 1).ToString(), _lstDoanhThu[i].DoanhThuGiaiDoan.TenGiaiDoan, String.Format("{0:#,##0;Nothing}", _lstDoanhThu[i].Thu), String.Format("{0:#,##0;Nothing}", _lstDoanhThu[i].ChiLuong), String.Format("{0:#,##0;Nothing}", _lstDoanhThu[i].ChiVLXD), String.Format("{0:#,##0;Nothing}", _lstDoanhThu[i].Loi), String.Format("{0:#,##0;Nothing}", _lstDoanhThu[i].Lo));
                tongthu += _lstDoanhThu[i].Thu;
                tongchi += _lstDoanhThu[i].ChiLuong + _lstDoanhThu[i].ChiVLXD;
            }
            
            doanhthu = tongthu - tongchi;

            rptThongKeDoanhThu rpt = new rptThongKeDoanhThu();
            rpt.DataDefinition.FormulaFields["TenCongTrinh"].Text = "'" + _congtrinh + "'";
            rpt.DataDefinition.FormulaFields["NgayThangNam"].Text = "'Ngày " + DateTime.Now.Day.ToString() + " tháng " + DateTime.Now.Month.ToString() + " năm " + DateTime.Now.Year.ToString() + "'";
            rpt.DataDefinition.FormulaFields["TongThu"].Text = "'" + String.Format("{0:#,##0;Nothing}", tongthu) + "'";
            rpt.DataDefinition.FormulaFields["TongChi"].Text = "'" + String.Format("{0:#,##0;Nothing}", tongchi) + "'";
            rpt.DataDefinition.FormulaFields["DoanhThu"].Text = "'" + String.Format("{0:#,##0;Nothing}", doanhthu) + "'";

            rpt.SetDataSource(dt);
            crystalReportViewer1.ReportSource = rpt;
        }
    }
}
